<?php

/*
 * 公司控制器
 * @author turtle
 * create_time 2017-01-04
 */
 

namespace App\Admin\Controllers\Company;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Constraint;
use Intervention\Image\Facades\Image;
use App\Admin\Controllers\Controller;
use App\Admin\Models\Role;
use App\Admin\Models\Permission;
use App\Admin\Models\PermissionRole;
use App\Admin\Models\Logs;
use App\Admin\Models\Company;
use App\Admin\Models\User;

class CompanyController extends Controller {

    /*
     * 公司公告页面
     * @author turtle
     * create_time 2017-01-04
     */

    public function announcement() {
        $bool = User::checkUserPermissions('add_announce');
        if (!$bool) {                                    
            return redirect()                            
                ->route('admin.dashboard')               
                ->with([                                 
                    'message' => "您没有拥有该权限，请联系管理员",
                    'alert-type' => 'error',             
                ]);                                      
        }
        $announcement = Company::where('title_type', '公司公告')->first();
        $datas['announcement'] = $announcement;
        return view('admin.announcement', $datas);
    }

    /*
     * 公司制度页面
     * @author turtle
     * create_time 2017-01-04
     */

    public function system() {
        $bool = User::checkUserPermissions('add_system');
        if (!$bool) {                                    
            return redirect()                            
                ->route('admin.dashboard')               
                ->with([                                 
                    'message' => "您没有拥有该权限，请联系管理员",
                    'alert-type' => 'error',             
                ]);                                      
        }

        $system = Company::where('title_type', '公司制度')->first();
        $datas['system'] = $system;
        return  view('admin.system', $datas);
    }

    /*
     * 添加/编辑
     * @author turtle
     * create_time 2017-01-04
     */
    public function edit(Request $request) {
        $data['title'] = $request->input('title');
        $title_type = $request->input('title_type');
        $company = Company::where('title_type', $title_type)->first();
        if (empty($company)) {
            $data['title_type'] = $title_type;
            $data['created_at'] = date('Y-m-d H:m:s');
            $data['updated_at'] = date('Y-m-d H:m:s');
            $rs = Company::insert($data);
            if ($rs) {
                $log = "添加".$title_type."成功";
                Logs::addLog($log);
                return redirect()
                    ->route('admin.dashboard')
                    ->with([                                                                                                                                                  
                        'message' => "添加成功",
                        'alert-type' => 'success',
                    ]);

            } else {
                $log = "添加".$title_type."失败";
                Logs::addLog($log);
                return redirect()
                    ->route('admin.dashboard')
                    ->with([                                                                                                                                                  
                        'message' => "抱歉，添加失败",
                        'alert-type' => 'error',
                    ]);
            }

        } else {
            $data['updated_at'] = date('Y-m-d H:m:s');
            $rs = Company::where('title_type', $title_type)->update($data);
            if ($rs === false) {
                $log = "编辑".$title_type."失败";
                Logs::addLog($log);
                return redirect()
                    ->route('admin.dashboard')
                    ->with([                                                                                                                                                  
                        'message' => "抱歉，编辑失败",
                        'alert-type' => 'error',
                    ]);

            } else {
                $log = "编辑".$title_type."成功";
                Logs::addLog($log);
                return redirect()
                    ->route('admin.dashboard')
                    ->with([                                                                                                                                                  
                        'message' => "编辑成功",
                        'alert-type' => 'success',
                    ]);

            }
        }
    }

}
